package com.ruoyi.wq.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.Condition;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wq.domain.WqVote;
import com.ruoyi.wq.domain.vo.WqVoteVO;
import com.ruoyi.wq.mapper.WqVoteMapper;
import com.ruoyi.wq.service.IWqVoteService;

/**
 * 投票Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-09
 */
@Service
public class WqVoteServiceImpl extends ServiceImpl<WqVoteMapper, WqVote> implements IWqVoteService {
	
    @Autowired
    private WqVoteMapper wqVoteMapper;
    
    @Autowired
    private ISysUserService sysUserService;
    
    @Autowired
	private RedisCache redisCache;

    /**
     * 查询投票
     * 
     * @param id 投票主键
     * @return 投票
     */
    @Override
    public WqVote selectWqVoteById(String id)
    {
        return wqVoteMapper.selectWqVoteById(id);
    }

    /**
     * 查询投票列表
     * 
     * @param wqVote 投票
     * @return 投票
     */
    @Override
    public List<WqVote> selectWqVoteList(WqVote wqVote)
    {
        return wqVoteMapper.selectWqVoteList(wqVote);
    }

    /**
     * 新增投票
     * 
     * @param wqVote 投票
     * @return 结果
     */
    @Override
    public int insertWqVote(WqVote wqVote)
    {
        wqVote.setCreateTime(DateUtils.getNowDate());
        return wqVoteMapper.insertWqVote(wqVote);
    }

    /**
     * 修改投票
     * 
     * @param wqVote 投票
     * @return 结果
     */
    @Override
    public int updateWqVote(WqVote wqVote)
    {
        wqVote.setUpdateTime(DateUtils.getNowDate());
        return wqVoteMapper.updateWqVote(wqVote);
    }

    /**
     * 批量删除投票
     * 
     * @param ids 需要删除的投票主键
     * @return 结果
     */
    @Override
    public int deleteWqVoteByIds(String[] ids)
    {
        return wqVoteMapper.deleteWqVoteByIds(ids);
    }

    /**
     * 删除投票信息
     * 
     * @param id 投票主键
     * @return 结果
     */
    @Override
    public int deleteWqVoteById(String id)
    {
        return wqVoteMapper.deleteWqVoteById(id);
    }

	@Override
	public TableDataInfo getPageList(WqVoteVO wqVote) {
		IPage<WqVoteVO> page = baseMapper.selectPageList(Condition.getPage(wqVote), wqVote);
		List<WqVoteVO> list = page.getRecords();
		for(WqVoteVO vote : list){
			if(StringUtils.isNotBlank(vote.getImage())){
				String photo = vote.getImage().split(",")[0];
				vote.setImage(photo);
			}
		}
		return Condition.getTableDataInfo(list, page.getTotal());
	}

	@Override
	public WqVoteVO getDetail(String id) {
		WqVote wqVote = baseMapper.selectById(id);
		// 查询前一名
		WqVote firstVote = baseMapper.queryFirstVote(wqVote);
		WqVoteVO wqVoteVO = new WqVoteVO();
		BeanUtils.copyProperties(wqVote, wqVoteVO);
		if(firstVote == null){
			wqVoteVO.setChaVoteNum(0L);
		}else{
			Long chaVoteNum = firstVote.getVoteNum() - wqVote.getVoteNum();
			wqVoteVO.setChaVoteNum(chaVoteNum);
		}
		return wqVoteVO;
	}

	@Override
	public void saveVote(WqVoteVO wqVote) {
		if(StringUtils.isBlank(wqVote.getUid())){
			throw new ServiceException("缺少参数");
		}
		Boolean hasKey = redisCache.hasKey(wqVote.getUid());
		if(hasKey){
			Long uid = redisCache.incr(wqVote.getUid());
			if(uid < 2){
				SysUser user = sysUserService.getById(SecurityUtils.getUserId());
				if(user.getVisitNum() < 1){
					// 趣豆不足
        			throw new ServiceException("参加评选需要【200】趣豆，请充值",888);
				}
				
				wqVote.setNickName(user.getNickName());
				wqVote.setCity(user.getCity());
				wqVote.setAvatar(user.getAvatar());
				wqVote.setVoteNum(0L);
				baseMapper.insert(wqVote);
			}
			redisCache.deleteObject(wqVote.getUid());
		}
	}

}
